﻿
using Abp.Application.Services.Dto;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.JitAlcCodes;
using KDS.Mes.Mes.JitAlcCodes.Dtos;
using KDS.Mes.Web.Models.JitAlcCodes;
using KDS.Mes.Web.Controllers;
using System;
using System.Threading.Tasks;
using System.Web.Mvc;
using KDS.Mes.Mes.AlcCodes;
using KDS.Mes.Mes.Cartypes;
using KDS.Mes.Mes.Styles;
using KDS.Mes.Mes.Lines;
using KDS.Mes.Mes.Storages;
using Abp.Web.Models;
using System.Collections.Generic;
using KDS.Mes.Mes.Cartypes.Dtos;
using KDS.Mes.Mes.Styles.Dtos;
using System.Linq;
using KDS.Mes.Mes.Lines.Dtos;
using KDS.Mes.Mes.Storages.Dtos;
using KDS.Mes.Mes.AlcCodes.Dtos;

namespace KDS.Mes.Web.Areas.Controllers
{
    public class JitAlcCodesController : MesControllerBase
    {

        private readonly IJitAlcCodeAppService _jitAlcCodeAppService;
        private readonly IAlcCodeAppService _alcCodeAppService;
        private readonly ICartypeAppService _cartypeAppService;
        private readonly IStyleAppService _styleAppService;
        private readonly ILineAppService _lineAppService;
        private readonly IStorageAppService _storageAppService;

        public JitAlcCodesController(
            IJitAlcCodeAppService jitAlcCodeAppService,
            IAlcCodeAppService alcCodeAppService,
            ICartypeAppService cartypeAppService,
            IStyleAppService styleAppService,
            ILineAppService lineAppService,
            IStorageAppService storageAppService
            )
        {
            _jitAlcCodeAppService = jitAlcCodeAppService;
            _alcCodeAppService = alcCodeAppService;
            _cartypeAppService = cartypeAppService;
            _styleAppService = styleAppService;
            _lineAppService = lineAppService;
            _storageAppService = storageAppService;
        }

        public async Task<ActionResult> Index()
        {
            var cartypes = new List<ComboboxItemDto>();
            var getCartypesOutput = await _cartypeAppService.GetCartypes(new GetCartypeInput());
            cartypes = getCartypesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.CartypeName)).ToList();

            var styles = new List<ComboboxItemDto>();
            var getStylesOutput = await _styleAppService.Getstyles(new GetStyleInput());
            styles = getStylesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.StyleName)).ToList();

            var alcCodes = new List<ComboboxItemDto>();
            var getAlcCodesOutput = await _alcCodeAppService.GetalcCodes(new GetAlcCodeInput());
            alcCodes = getAlcCodesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.ALCCode)).ToList();

            var lines = new List<ComboboxItemDto>();
            var getLinesOutput = await _lineAppService.GetLines(new GetLineInput());
            lines = getLinesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.LineName)).ToList();

            var storages = new List<ComboboxItemDto>();
            var getStoragesOutput = await _storageAppService.GetStorages(new GetStorageInput());
            storages = getStoragesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.StorageCode)).ToList();

            cartypes.Insert(0, new ComboboxItemDto("", ""));
            styles.Insert(0, new ComboboxItemDto("", ""));
            alcCodes.Insert(0, new ComboboxItemDto("", ""));
            lines.Insert(0, new ComboboxItemDto("", ""));
            storages.Insert(0, new ComboboxItemDto("", ""));
            var model = new JitAlcCodesViewModel
            {
                FilterText = Request.QueryString["filterText"],
                Cartypes = cartypes,
                Styles = styles,
                AlcCodes = alcCodes,
                Lines = lines,
                Storages = storages
            };

            return View(model);
        }

        [DontWrapResult]
        public JsonResult GetAllJitAlcCodes(
           int limit, int offset, string sortfiled, string sortway,
           string cartypeId, string styleId, string alccodeId, string lineId, string storageId)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetJitAlcCodeInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
                CartypeId = cartypeId,
                StyleId = styleId,
                AlcCodeId = alccodeId,
                LineId = lineId,
                StorageId = storageId
            };
            var pagedTasks = _jitAlcCodeAppService.GetPagedJitAlcCodes(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[AbpMvcAuthorize(JitAlcCodeAppPermissions.JitAlcCode_CreateJitAlcCode,JitAlcCodeAppPermissions.JitAlcCode_EditJitAlcCode)]
        [AbpMvcAuthorize(AppPermissions.Pages_Bom_JitAlcCodes_Create, AppPermissions.Pages_Bom_JitAlcCodes_Edit)]
        public async Task<PartialViewResult> CreateOrEditJitAlcCodeModal(int? id)
        {
            var input = new NullableIdDto<int> { Id = id };
            var output = await _jitAlcCodeAppService.GetJitAlcCodeForEditAsync(input);
            var viewModel = new CreateOrEditJitAlcCodeModalViewModel(output);
            return PartialView("_CreateOrEditJitAlcCodeModal", viewModel);
        }


    }
}